<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="宠物名字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select clearable v-model="query.state" placeholder="状态">
                        <el-option label="下架" value="0"></el-option>
                        <el-option label="上架" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSale" :disabled="this.sels.length===0">上架</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表
            v-loading:开启加载效果

        -->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="宠物名字" width="120" >
            </el-table-column>
            <el-table-column prop="saleprice" label="售价" width="120" >
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" width="120" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" width="120" >
            </el-table-column>
            <el-table-column prop="costprice" label="成本" width="120" >
            </el-table-column>
            <el-table-column prop="state" label="状态" min-width="80" >
                <template scope="scope">
                    <span v-if="scope.row.state==0" style="color: #8c939d">下架</span>
                    <span v-if="scope.row.state==1" style="color: #13ce66">上架</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                           :page-size="query.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增、编辑界面
            :rules:定义验证规则


        -->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="宠物标题" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="销售价格" >
                    <el-input v-model="addForm.saleprice" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="成本价格" >
                    <el-input v-model="addForm.costprice" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="宠物靓照">
                    <el-upload
                            class="upload-demo"
                            action="/api/fastDfs/upload"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="宠物详情" >
                   <!-- <el-input v-model="addForm.detail.intro" auto-complete="off"></el-input>-->
                    <quill-editor v-model="addForm.detail.intro" :options="quillConfig"></quill-editor>
                </el-form-item>
                <el-form-item label="领养须知" >
                   <!-- <el-input v-model="addForm.detail.adoptNotice" auto-complete="off"></el-input>-->
                    <quill-editor v-model="addForm.detail.adoptNotice" :options="quillConfig"></quill-editor>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>


    </section>
</template>

<script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    /*导入 配置文件*/
    import quillConfig from '../../common/js/quill-config'

    export default {
        components:{//引入并加载组件
            quillEditor
        },
        data(){
            return {
                quillConfig:quillConfig,
                fileList:[],
                listLoading:false,//开启加载效果
                listData:[],
                sels:[],
                total:0, //数据的总条数
                query:{//分页条件
                    pageSize:5,
                    currentPage:1,
                    keywords:'',
                    state:null

                },
                addFormVisible:false, //默认隐藏弹出框
                addForm:{// 用于数据双向绑定对象
                    name:'',
                    resources:'',
                    state:0,
                    saleprice:0,
                    costprice:0,
                    type_id:1,
                    detail:{
                        adoptNotice:"",
                        intro:""
                    }
                },
                formVisible:false,
                dataForm:{//数据展示
                    reason:"",

                },
                addFormRules: {
                    /*
                        验证规则
                           要求： required: true   必填
                           消息提示：出错时候 提示什么信息 message: '请输入姓名'
                           触发规则：trigger: 'blur'

                    */
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入部门编号', trigger: 'blur' }
                    ]
                },
                addLoading:false,

            }
        },
        methods:{
            //文件上传成功回调
            handleSuccess(response, file, fileList){
                //如果上传多张图片，把图片拼接在一起,使用“,”分割
                //console.debug(fileList);
                let str="";
                for(var i=0;i<fileList.length;i++){
                    if(i==fileList.length-1){//最后一个元素
                        str+=fileList[i].response.resultObj;
                    }else {
                        str+=fileList[i].response.resultObj+",";
                    }
                }

                this.addForm.resources=str;
            },
            //文件删除
            handleRemove(file, fileList) {

            },
            // index:当前数据的行号  row:就是当前的数据
            handleEdit(index,row){
                //数据拷贝   回显
                this.addForm=Object.assign({}, row);
                this.fileList=[];
                //图片数据的回显
                //this.fileList=[{name:"xx",url:"xx"},{}];
                let picArr =row.resources.split(",");
                for (let i =0;i<picArr.length;i++){
                    let obj ={
                        name:"图片"+(i+1),
                        url:"http://47.95.117.210:8888"+picArr[i]
                    }
                    this.fileList.push(obj);
                }
                console.debug(this.fileList);
                //打开弹出框
                this.addFormVisible=true;
            },
            //给数据前面的选择按钮添加事件
            selsChange(val){
                //val 就是用户勾选的数据
                this.sels=val;
            },
            //性别显示转换
            formatSex: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
            },
            loadListData(){//远程请求列表数据
                //发送请求获取数据  在main.js 添加 axios.default.baseUrl='/api'
                this.$http.post("/pet",this.query).then(res=>{
                    this.listData=res.data.data;
                    this.total=res.data.total;
                })

            },
            onSale(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认要上架选中宠物？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/pet/onSale",ids).then(res=>{
                        this.listLoading = false;
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: '上架成功',
                                type: 'success'
                            });
                        }else {
                            this.$message.error('网络异常');
                        }
                        //重新加载数据
                        this.loadListData();
                    })
                });
            },
            //批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/shop",ids).then(res=>{
                        this.listLoading = false;
                        console.debug(res)
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else {
                            this.$message.error('网络异常');
                        }
                        //重新加载数据
                        this.loadListData();
                    })
                }).catch(() => {

                });


            },
            //点击分页触发的方法
            handleCurrentChange(val){
                //val 就是点击翻页的时候 当前页页码
                this.query.currentPage=val;
                this.loadListData();
            },
            //查询的方法
            search(){
                //初始化开始的页码
               this.query.currentPage=1;
                //重新加载数据
               this.loadListData();
            },
            //新增的方法
            handleAdd(){
                //清空表单
                this.addForm={// 用于数据双向绑定对象
                    name:'',
                    resources:'',
                    state:0,
                    saleprice:0,
                    costprice:0,
                    type_id:1,
                    detail:{
                        adoptNotice:"",
                        intro:""
                    }
                }
                //清空上传的图片
                this.fileList=[];
                //打开弹出框
                this.addFormVisible=true;
            },
            //删除的方法  index:当前数据的行号    row:当前行数据
            handleDel(index,row){
               //1.删除的确认框
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(()=>{//确认删除的按钮
                    this.listLoading = true;
                    //获取删除数据id
                    let id= row.id;
                    //发送请求 "/dept/3"
                    this.$http.delete("/shop/"+id).then(res=>{
                        this.listLoading = false;
                        console.debug(res)
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else {
                            this.$message.error('网络异常');
                        }
                        //重新加载数据
                        this.loadListData();
                    })

                }).catch(()=>{//取消删除回调函数
                    this.$message('已取消删除');
                })
            },
            addSubmit(){
               //开启加载效果
                this.addLoading=true;
                console.debug(this.addForm)
                //发送请求
                this.$http.put("/pet",this.addForm).then(res=>{
                    console.debug(res)
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }
                    //关闭加载效果
                    this.addLoading=false;
                    //关闭弹出框
                    this.addFormVisible=false;
                    //重新加载列表
                    this.loadListData();
                })
            }

        },
        mounted(){
            //页面加载完毕以后立即执行此方法
            this.loadListData();
        }

    }

</script>

<style>



</style>